<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Mhs extends CI_Controller {

    function __construct() { //import class
        parent::__construct();
        $this->load->helper('form', 'url');
        $this->load->model('all');
    }

    function trulog() { //cek session login
        $log_in = $this->session->userdata('log_in');
        if (!isset($log_in) || $log_in != TRUE) {
            redirect('mhs/index');
        }
    }

    function index() {
        $hsl['rangking'] = $this->all->rangking();
        $this->load->view('template/head');
        $this->load->view('mhs/content', $hsl);
        $this->load->view('template/end');
    }

    function amikom() { //login dengan database amikom
        $this->load->library('xmlrpc');
        $this->xmlrpc->server('http://www.amikom.ac.id/index.php/service/');
        $this->xmlrpc->method('login');
        $data['nim'] = $this->input->post('nim');
        $data['password'] = $this->input->post('password');
        $request = array(
            $nim = $this->security->xss_clean($data['nim']),
            $password = $this->security->xss_clean($data['password'])
        );
        $this->xmlrpc->set_debug(false);
        $this->xmlrpc->request($request);
        $this->xmlrpc->send_request();
    }

    function login() { //login
        $rpc = $this->amikom();
        $respon = json_decode($this->xmlrpc->display_response());
        if ($respon->status == 1) {
            $this->all->store();
            $user = array(
                'nim' => $this->input->post('nim'),
                'log_in' => TRUE,
                'status' => $respon->status,
                'alumni' => $respon->alumni
            );
            $this->session->set_userdata($user);
            redirect('mhs/roletime');
        } else {
            redirect('mhs/index');
        }
    }

    function home() { //halaman home
        $this->trulog();
        $iased['daaa'] = $this->all->nama();
        $this->load->view('template/head');
        $this->load->view('mhs/home');
        $this->load->view('mhs/right', $iased);
        $this->load->view('mhs/menu');
        $this->load->view('template/end');
    }

    function roletime() { //role polling berdasarkan waktu
        $this->trulog();
        $today = $this->all->today();
        $open = $this->all->open();
        $close = $this->all->close();
        $a = $today[0]['CURRENT_DATE()'];
        $b = $open[0]['open'];
        $c = $close[0]['close'];
        if ($a >= $b and $a <= $c) {
            redirect('mhs/polling');
        } else {
            redirect('mhs/time');
        }
    }

    function polling() { //role polling berdasarkan status dan alumni
        $this->trulog();
        $status = $this->session->userdata('status');
        $alumni = $this->session->userdata('alumni');
        if ($status == 1 && $alumni == 0) {
            $cek = $this->all->cekpol();
            if ($cek == TRUE) {
                $isi['data'] = $this->all->kandidat();
                $iased['daaa'] = $this->all->nama();
                $this->load->view('template/head');
                $this->load->view('mhs/polling', $isi);
                $this->load->view('mhs/polright', $iased);
                $this->load->view('mhs/menu');
                $this->load->view('template/end');
            } else {
                redirect('mhs/notice');
            }
        } else {
            redirect('mhs/alumni');
        }
    }

    function poll() { //sukses menyimpan pilihan polling
        $this->trulog();
        $p = $this->input->post('pilihan');
        $pi = $this->security->xss_clean($p);
        if ($pi != '') {
            $this->all->polling();
            $iased['daaa'] = $this->all->nama();
            redirect('mhs/success');
        } else {
            redirect('mhs/roletime');
        }
    }

    function alumni() {
        $this->trulog();
        $iased['daaa'] = $this->all->nama();
        $this->load->view('template/head');
        $this->load->view('mhs/alumni');
        $this->load->view('mhs/right', $iased);
        $this->load->view('mhs/menu');
        $this->load->view('template/end');
    }

    function time() {
        $this->trulog();
        $sh['show'] = $this->all->showtime();
        $iased['daaa'] = $this->all->nama();
        $this->load->view('template/head');
        $this->load->view('mhs/time', $sh);
        $this->load->view('mhs/right', $iased);
        $this->load->view('mhs/menu');
        $this->load->view('template/end');
    }

    function notice() { //notif telah memilih
        $this->trulog();
        $iased['daaa'] = $this->all->nama();
        $sed['lock'] = $this->all->nama();
        $this->load->view('template/head');
        $this->load->view('mhs/notice', $sed);
        $this->load->view('mhs/right', $iased);
        $this->load->view('mhs/menu');
        $this->load->view('template/end');
    }

    function success() { //data berhasil disimpan
        $this->trulog();
        $iased['daaa'] = $this->all->nama();
        $this->load->view('template/head');
        $this->load->view('mhs/success');
        $this->load->view('mhs/right', $iased);
        $this->load->view('mhs/menu');
        $this->load->view('template/end');
    }

    function logout() { //logout
        $this->trulog();
        $this->session->unset_userdata('nim');
        $this->session->unset_userdata('log_in');
        $this->session->unset_userdata('status');
        $this->session->unset_userdata('alumni');
        redirect('mhs');
    }

}

/* End of file mhs.php */
/* Location: ./application/controllers/mhs.php */